<?php

class LoginPresenter extends BasePresenter
{
	
	protected function createComponentLoginForm()
	{
		$form = new NAppForm;
		$form->addText('email', 'E-mail:')
			->addRule(NForm::FILLED, 'Zadejte prosím E-mail.');

		$form->addPassword('heslo', 'Heslo:')
			->addRule(NForm::FILLED, 'Zadejte prosím heslo.');

		$form->addCheckbox('remember', 'Pamatovat si mě');
		
		$form->addSubmit('login', 'Přihlásit');

		$form->onSubmit[] = callback($this, 'loginFormSubmitted');
		return $form;
	}



	public function loginFormSubmitted($form)
	{
		
		try {
			$values = $form->values;
			if ($values['remember']) {
				$this->getUser()->setExpiration('+ 14 days', FALSE);
			} else {
				$this->getUser()->setExpiration('+ 20 minutes', TRUE);
			}
			
			$this->getUser()->login($values['email'], $values['heslo']);
			$this->redirect(':Admin:Soutez:');

		} catch (NAuthenticationException $e) {
			$form->addError($e->getMessage());
		}
	}
		
	public function actionLogout()
	{
		$this->user->logout();
		$this->flashMessage('Byl jste odhlášen.');
		$this->redirect(':Login:');
	}

}
